extends Node

var utils = null
var ctrl = null
var diff
var base = null
var path = null
var window
var recordWindow
var shop
var research
var charaBuildBonus = 0
var shieldBonus = 1
var endlessopt:bool = false
var expert
var step:int
var gGB2
func _ready():
	pass
func _init():
	call_deferred("init")
func _connect():
	pass
	
func init():
	if globalData.infoDs.has("g_AneHekBase"):
		base = globalData.infoDs["g_AneHekBase"]
		path = chaData.infoDs["cTDW"].dir
	if base != null:
		#初始化
		utils = globalData.infoDs["g_AneHekUtils"]
		ctrl = globalData.infoDs["g_AneHek"]
		#base.connect("onStartGame",self,"link")
		gGB2 = globalData.infoDs["g_AneHekGB2"]

#难度加成
func changediff():
	#0层难度：(1.7/(e^(0.09*(10-(x-20)))+1))*0.6
	#161难度：(1/(e^(0.1*(10-(x-12)))+1))*2.5
	#stepdiff = (edadd - stadd) / 6.0
    #for i in range(6):
	#    lst[i] = (stepdiff * i) + stadd + stepdiff * 0.14
	#    led[i] = lst[i] + stepdiff * 0.86
    #led[5] = edadd
	#add = (step - int(step / 27) * 27) / 27.0 * (led[int(step/27)] - lst[int(step/27)]) + lst[int(step/27)]
	#print(step)
	#print(diff)
	#var pw = 3
	"""
	if diff > 3:
		if diff <= 20:
			pw = 3 - (diff - 3) * 0.1 - int(diff/5) * 0.1
		else:
			pw = 1 - (diff - 20) * max(0.005,(0.04 - (diff - 20) * 0.005)) - int((diff-20)/5) * 0.005
		pw = max(0.1,pw)
		add = 0.1 + pow(2.718,-1.0/(diff-3)) / pw
	else:
		add = 0.1 + diff * 0.023
	var maxadd = add * 2.5
	var startadd = add / 2
	if step <= 161:
		add = (maxadd - startadd) * (step / 161.0) + startadd
	else:
		add = maxadd + (step - 161) * 0.003 + int((step-161)/5) * 0.01
	"""
	step = sys.main.guankaMsg.lvStep - 2 + gGB2.count*161
	var diff = int(base.getSetting("difficult",1))
	var defadd = int(diff / 10) * 10 + diff + int(step / 2)
	#print(defadd)	
	var add:float = 0
	var stadd = (1.7 / ( pow(2.718,0.09 * ( 10 - (diff - 20))) + 1)) * 0.6
	var edadd = (1.0 / ( pow(2.718,0.1 * ( 10 - (diff - 12))) + 1)) * (3 - int(diff / 15) * 0.1)
	#print(edadd)
	var stepdiff = (edadd - stadd) / 6.0
	var lst = [0,0,0,0,0,0]
	var led = [0,0,0,0,0,0]
	for i in range(6):
		lst[i] = stepdiff * i + stadd
		if i:
			lst[i] += stepdiff * 0.5
		led[i] = lst[i] + stepdiff * 0.5
	led[5] = edadd
	if step <= 161:
		var cnt = int(step/27)
		add = (step - cnt * 27) / 27.0 * (led[cnt] - lst[cnt]) + lst[cnt]
	else:
		defadd += int(step - 161)
		add = edadd + (step - 161) * 0.003 + int((step-161)/5) * 0.01
	if step % 27 == 0 || step == 161:
		createboss(step,diff)
	#print(add)
	if diff > 14 && step == 28:
		sys.newBaseMsg("警告","从本层开始，后面将无血量保护机制，请谨慎排兵布局！")
	if diff > 14 && step <= 27:
		for i in utils.getAllChas(2):
			#i.attAdd.maxHpL += add * 0.6
			i.addBuff(XLBH.new(1,add * 0.6))
	for i in utils.getAllChas(1):
		i.attAdd.atkL += add
		i.attAdd.mgiAtkL += add
		i.attAdd.maxHpL += add
		i.attAdd.def += defadd
		i.attAdd.mgiDef += defadd
		i.attAdd.cd += add / 1.5
		i.attAdd.spd += add / 1.5
		if diff >= 15:
			#i.attAdd.spd += add / 3
			i.attAdd.defR += add / 2.35
			i.attAdd.atkR += add / 2.35
		yield(sys.get_tree().create_timer(0.1), "timeout")
		i.plusHp(i.att.maxHp)
	if ctrl.layertxt != null:
		ctrl.layertxt.text += " |当前难度：%d "%int(diff)
		ctrl.layertxt.text += "|当前敌方：血量 & 双攻 +{0}%  ".format({"0":float(int(add*1000)/10.0)})
		ctrl.layertxt.text += "|双速+{0}% ".format({"0":float(int(add/1.5*1000)/10.0)})
		if diff>=15:
			ctrl.layertxt.text += "|增/减伤+{0}%".format({"0":float(int(add/2.35*1000)/10.0)})
		ctrl.layertxt.text += "|双防+%d "%int(defadd)


#创建boss(待更新)
func createboss(step,diff):
	return
	var n = 1
	if diff > 5: n = 2
	if diff > 10: n = 3
	var lvs = int(step/27)
	if step == 161:lvs = 6
	#if base.getSetting("expertMode",false):
	#	gGB2.clearAll()
	var pos = Vector2(9,2)
	for i in range(2):
		if i:pos += Vector2(0,1)
		var cha =  sys.main.matCha(pos)
		if cha != null:
			sys.main.delMatChara(cha)
		var id = "cex___AneHekBoss"
		if lvs == 1:
			if sys.rndPer(50):
				id += "1"
			else:
				id += "2"
		if lvs == 2:
			id += str(sys.rndRan(3,5))
		if lvs == 3:
			id += str(sys.rndRan(3,8))
		if lvs == 4:
			id += str(sys.rndRan(6,8))
		if lvs == 5:
			id += "9"
		if lvs == 6:
			id += "13"
		var ncha = sys.main.newChara(id, 2)
		sys.main.map.add_child(ncha)
		sys.main.setMatCha(pos, ncha)
		#for j in range(n):
		#	ncha.addItem(sys.newItem(utils.getRndItem())) 
		if lvs == 1 || lvs == 5 || lvs == 6:
			break
		yield(sys.get_tree().create_timer(0.02), "timeout")
	var tmpid = ""
	for i in range(min(5,lvs)):
		tmpid += "1"
	pos = Vector2(5,2)
	for i in range(2):
		if i:pos += Vector2(0,1)
		var cha =  sys.main.matCha(pos)
		if cha != null:
			sys.main.delMatChara(cha)
		var id = sys.rndListItem(utils.x5) + tmpid
		var file = File.new()
		while !file.file_exists(path + "/" + id + "/" + id + ".gd"):
			id = sys.rndListItem(utils.x5) + tmpid
			yield(sys.get_tree().create_timer(0.02), "timeout")
		var ncha = sys.main.newChara(id, 2)
		sys.main.map.add_child(ncha)
		sys.main.setMatCha(pos, ncha)
		for j in range(n):
			ncha.addItem(sys.newItem(utils.getRndItem())) 
		yield(sys.get_tree().create_timer(0.02), "timeout")

class XLBH:
	extends Buff
	var canremove:bool = false
	func _init(lv,add):
		attInit()
		id = "XLBH"
		att.maxHpL = add
	func _connect():
		masCha.connect("onHurt",self,"run")
	func run(atkinfo):
		yield(sys.get_tree().create_timer(0.01),"timeout")
		if atkinfo.hurtVal > masCha.att.maxHp * 0.8:
			atkinfo.hurtVal = masCha.att.maxHp * 0.8